/*
	Enlarge for jQuery v1.1
	Abel Yao, 2013
	http://www.abelcode.com/
*/

(function($)
{
	// 榛樿鍙傛暟
	var defaults = 
	{
		// 榧犳爣閬僵灞傛牱寮�
		shadecolor: "#FFD24D",
		shadeborder: "#FF8000",
		shadeopacity: 0.5,
		cursor: "move",
		
		// 澶у浘澶栧眰鏍峰紡
		layerwidth: 400,
		layerheight: 300,
		layerborder: "#DDD",
		fade: true,
		
		// 澶у浘灏哄
		largewidth: 1280,
		largeheight: 960
	}
	
	// 鎻掍欢鍏ュ彛
	var enlarge = function(option)
	{
		// 鍚堝苟鍙傛暟
		option = $.extend({}, defaults, option);
		
		// 寰幆澶勭悊
		$(this).each(function() 
		{
			var self = $(this).css("position", "relative");
			var img = self.children().first();
			
			// 璁＄畻澶у皬鍥句箣闂寸殑姣斾緥
			var ratio =
			{
				x: img.width() / option.largewidth,
				y: img.height() / option.largeheight
			}
			
			// 瀹氫箟涓€浜涘昂瀵�
			var size = 
			{
				// 璁＄畻榧犳爣閬僵灞傜殑澶у皬
				shade:
				{
					width: option.layerwidth * ratio.x - 2,
					height: option.layerheight * ratio.y - 2
				}
			}
			
			// 鍒涘缓榧犳爣閬僵灞�
			var shade = $("<div>").css(
			{
				"position": "absolute",
				"left": "0px",
				"top": "0px",
				"background-color": option.shadecolor,
				"border": "1px solid " + option.shadeborder,
				"width": size.shade.width,
				"height": size.shade.height,
				"opacity": option.shadeopacity,
				"cursor": option.cursor
			});
			shade.hide().appendTo(self);
			
			// 鍒涘缓澶у浘鍜屾斁澶у浘灞�
			var large = $("<img>").css(
			{
				"position": "absolute",
				"display": "block",
				"width": option.largewidth,
				"height": option.largeheight
			});
			var layer = $("<div>").css(
			{
				"position": "absolute",
				"left": self.width() + 15,
				"top": -1,
				"background-color": "#111",
				"overflow": "hidden",
				"width": option.layerwidth,
				"height": option.layerheight,
				"border": "1px solid " + option.layerborder
			});
			large.attr("src", self.attr("href"));
			large.appendTo(layer);
			layer.hide().appendTo(self);
			
			// 涓嶅彲绉诲姩鐨勫崐寰勮寖鍥�
			var half = 
			{
				x: size.shade.width / 2,
				y: size.shade.height / 2
			}
			
			// 鏈夋晥鑼冨洿
			var area = 
			{
				width: self.innerWidth() - shade.outerWidth(),
				height: self.innerHeight() - shade.outerHeight()
			}
            
            // 瀵硅薄鍧愭爣
			var offset = self.offset();
			
			// 鏄剧ず鏁堟灉
			var show = function()
			{
                offset = self.offset();
				shade.show();
				if(option.fade) layer.stop().fadeIn(300);
				else layer.show();
			}
			
			// 闅愯棌鏁堟灉
			var hide = function()
			{
				shade.hide();
				layer.hide();
			}
			
			// 缁戝畾榧犳爣浜嬩欢
			self.mousemove(function(e)
			{
				// 榧犳爣浣嶇疆
				var x = e.pageX - offset.left;
				var y = e.pageY - offset.top;
				
				// 涓嶅湪鍥惧儚涓垯鐩存帴闅愯棌
				if(x < 0 || x > self.innerWidth()) return hide();
				if(y < 0 || y > self.innerHeight()) return hide();
				
				// 鍒ゆ柇鏄惁鍦ㄦ湁鏁堣寖鍥村唴
				x = x - half.x;
				y = y - half.y;
				
				if(x < 0) x = 0;
				if(y < 0) y = 0;
				if(x > area.width) x = area.width;
				if(y > area.height) y = area.height;
				
				// 閬僵灞傝窡闅忛紶鏍�
				shade.css(
				{
					left: x,
					top: y
				});
				
				// 澶у浘绉诲姩鍒扮浉搴斾綅缃�
				large.css(
				{
					left: (0 - x / ratio.x),
					top: (0 - y / ratio.y)
				});
			})
			.mouseenter(show)
			.mouseleave(hide);
        });
	}
	
	// 鎵╁睍鎻掍欢
	$.fn.extend(
	{
		enlarge: enlarge
	});
})(jQuery)